package day16;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UserTest {
    static Scanner sc=new Scanner(System.in);
    static List<User2> users=new ArrayList<>();
    static Integer id=0;
    static String userLogin;
    public static void main(String[] args) {
        One();
    }
    public static void One(){
        while(true){
            System.out.println("***XXX用户管理系统***");
            System.out.println("1.用户注册");
            System.out.println("2.用户登录");
            System.out.println("3.退出系统");
            System.out.println("*******************");
            System.out.print("请输入一级菜单对应的编号：");
            int n=sc.nextInt();
            switch (n){
                case 1:
                    Register();
                    break;
                case 2:
                    Login();
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("输入错误，请重新输入！");
                    continue;
            }
        }
    }

    public static void Two(String userLogin){
        System.out.println("***"+userLogin+"登录成功***");
        System.out.println("1.展示用户");
        System.out.println("2.修改密码");
        System.out.println("3.删除用户");
        System.out.println("4.用户退出");
        System.out.println("********************");
        one:
        while (true){
            System.out.print("请输入二级菜单对应的编号：");
            int n=sc.nextInt();
            switch (n){
                case 1:
                    ShowUser();
                    break;
                case 2:
                    ChangePassword();
                    break;
                case 3:
                    DeleteUser();
                    break;
                case 4:
                    break one;
                default:
                    System.out.println("输入错误，请重新输入!");
                    continue;
            }
        }
    }

    private static void DeleteUser() {
        System.out.print("请输入用户编号：");
        int id= sc.nextInt();
        //切记：增强for循环和迭代器不能一边遍历一边删除，否则会出现异常
        //由于迭代器底层，每次获取第一个元素（然后再删除第一个），如果你
        //删除了两次，当达到第一个元素时，就没有值，出现异常
        //面试题：有一个对象集合，我想删除其中的一个对象该怎么做？
//        User2 userDel=null;
//        for (User2 u:users){
//            if (u.getId()==id) userDel=u;
//            if (userDel!=null){
//                users.remove(userDel);
//                System.out.println(userDel.getUsername()+"删除成功！");
//            }
//        }
        //jdk1。8的特性，有一个Stream流，removeIf();
        users.removeIf(u->u.getId()==id);
    }

    private static void ChangePassword() {
    }

    private static void ShowUser() {
        System.out.println("序号\t编号\t用户\t密码\t");
        for (int i=0;i<users.size();i++){
            User2 u=users.get(i);
            System.out.println(i+1+"\t"+u.getId()+"\t"+u.getUsername()+"\t"+u.getPassword());
        }
    }

    public static void Register(){
        id++;
        System.out.print("请输入要注册的账号：");
        String account=sc.next();
        System.out.print("请输入密码：");
        String psssword=sc.next();
        users.add(new User2(id,account,psssword));
        System.out.println(account+"注册成功！");
    }
    public static void  Login(){
        System.out.print("请输入账号：");
        String account=sc.next();
        System.out.print("请输入密码：");
        String password=sc.next();
        for (User2 u:users){
            if (u.getUsername().equals(account) && u.getPassword().equals(password)){
                userLogin=u.getUsername();
                Two(userLogin);
                return;
            }
        }
    }
}

class User2{

    private int id;
    private String username;
    private String password;

    public User2(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}